package com.digitalchina.lxnsy.api;

import android.app.Activity;
import android.os.Handler;

import com.alibaba.fastjson.JSON;
import com.digitalchina.lxnsy.constant.Constant;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.GenerateOrderVO;
import com.digitalchina.lxnsy.toolkit.OkHttpUtil;
import com.digitalchina.lxnsy.xml.UserXML;
import com.lzy.okhttputils.model.HttpParams;

/**
 * ================================================
 * 项目名称：bigdata
 * 类描述：购物车API
 * 包名：com.digitalchina.bigdata.api
 * 创建人：白彦东
 * 创建时间：2016/10/20 10:32
 * 修改备注：
 * ================================================
 */

public class BusinessFarmSystem {


    /**
     * 获取购物车列表
     *
     * @param activity
     * @param handler
     */
    public static void getShoppingCartItems(Activity activity, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("userId", UserXML.getUserId(activity));

        OkHttpUtil.post(activity, URL.URL_GET_SHOPPING_CART_ITEMS, "", params, handler,
                MSG.MSG_GET_SHOPPING_CART_ITEMS_SUCCESS, MSG.MSG_GET_SHOPPING_CART_ITEMS_FIELD);

    }

    /**
     * 加入购物车
     *
     * @param activity
     * @param itemId       物品id
     * @param itemQuantity 数量
     * @param attrName     商品属性名称
     * @param orgAccId     商户账户id
     * @param poHandler
     */
    public static void addShoppingCartItem(Activity activity, String itemId, String itemQuantity, String attrName,
                                           String orgAccId, Handler poHandler) {

        HttpParams params = new HttpParams();
        params.put("userId", UserXML.getUserId(activity));
        params.put("itemId", itemId);
        params.put("itemQuantity", itemQuantity);
        params.put("attrName", attrName);
        params.put("orgAccId", orgAccId);
        params.put("appChannel", Constant.APP_CHANNEL); // 应用渠道 PC：0，移动端：1

        OkHttpUtil.post(activity, URL.URL_ADD_SHOPPING_CART_ITEM_AGSRV, "请稍候...", params, poHandler, MSG.MSG_ADD_SHOPPING_CART_ITEM_AGSRV_SUCCESS, MSG.MSG_ADD_SHOPPING_CART_ITEM_AGSRV_FIELD);

    }

    /**
     * 获取农产品详情
     *
     * @param activity
     * @param productId 农产品ID
     * @param handler
     */
    public static void getProductInfo(Activity activity, String productId, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("productId", productId);

        OkHttpUtil.post(activity, URL.URL_GET_PRODUCT_INFO, "", params, handler,
                MSG.MSG_GET_PRODUCT_INFO_SUCCESS, MSG.MSG_GET_PRODUCT_INFO_FIELD);
    }

    /**
     * 农产品列表
     *
     * @param activity
     * @param offset
     * @param limit
     * @param handler
     */
    public static void getProductList(Activity activity, String offset, String limit, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("category", ""); // 分类
        params.put("keywords", ""); // 查询关键字
        params.put("orderType", ""); // 排序类型
        params.put("offset", offset);
        params.put("limit", limit);

        OkHttpUtil.post(activity, URL.URL_PRODUCT_LIST, "", params, handler,
                MSG.MSG_PRODUCT_LIST_SUCCESS, MSG.MSG_PRODUCT_LIST_FIELD);
    }

    /**
     * 获取地址列表
     *
     * @param activity
     * @param handler
     */
    public static void getAddressList(Activity activity, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("useraccId", UserXML.getUserId(activity));


        OkHttpUtil.post(activity, URL.URL_ADDRESS_LIST_AGSRV, "", params, handler, MSG.MSG_ADDRESS_LIST_SUCCESS, MSG.MSG_ADDRESS_LIST_FIELD);

    }

    /**
     * 删除地址
     *
     * @param activity
     * @param addressId
     * @param handler
     */
    public static void deleteAddress(Activity activity, String addressId, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("addressId", addressId);

        OkHttpUtil.post(activity, URL.URL_ADDRESS_DELETE, "请稍候···", params, handler, MSG.MSG_ADDRESS_DELETE_SUCCESS, MSG.MSG_ADDRESS_DELETE_FIELD);
    }

    /**
     * 保存地址 如果为编辑地址 addressId则是必输项
     *
     * @param activity
     * @param userName
     * @param provinceCode
     * @param cityCode
     * @param countyCode
     * @param address
     * @param mobilePhone
     * @param addressId
     * @param handler
     */
    public static void editAddress(Activity activity, String addressId, String userName, String provinceCode,
                                   String cityCode, String countyCode, String address,
                                   String mobilePhone, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("addressId", addressId);
        params.put("useraccId", UserXML.getUserId(activity));
        params.put("userName", userName);
        params.put("provinceCode", provinceCode);
        params.put("cityCode", cityCode);
        params.put("countyCode", countyCode);
        params.put("address", address);
        params.put("mobilePhone", mobilePhone);


        OkHttpUtil.post(activity, URL.URL_ADDRESS_SAVE, "", params, handler, MSG.MSG_ADDRESS_EDIT_SUCCESS, MSG.MSG_ADDRESS_EDIT_FIELD);

    }


    /**
     * 快递费用计算
     *
     * @param activity
     * @param productId 商品ID
     * @param num       购买数量
     * @param handler
     */
    public static void computePostage(Activity activity, String productId, String num, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("productId", productId);
        params.put("num", num);

        OkHttpUtil.post(activity, URL.URL_COMPUTE_POSTAGE, "", params, handler, MSG.MSG_COMPUTE_POSTAGE_SUCCESS, MSG.MSG_COMPUTE_POSTAGE_FIELD);

    }


    /**
     * 快递费用计算
     *
     * @param activity
     * @param jsonStr  json格式的字符串[{num1,productId1},{num2,productId2},...]
     * @param handler
     */
    public static void computePostageForProductJson(Activity activity, String jsonStr, Handler handler) {

        HttpParams params = new HttpParams("products", jsonStr);

        OkHttpUtil.post(activity, URL.URL_COMPUTE_POSTAGE_FOR_PRODUCT_JSON, "", params, handler, MSG.MSG_COMPUTE_POSTAGE_FOR_PRODUCT_JSON_SUCCESS, MSG.MSG_COMPUTE_POSTAGE_FOR_PRODUCT_JSON_FIELD);

    }

    /**
     * 拆单并生成订单
     *
     * @param generateOrderVO
     */
    public static void generateOrder(GenerateOrderVO generateOrderVO) {

        HttpParams params = new HttpParams();
        params.put("buyerId", generateOrderVO.getBuyerId());
        params.put("buyerLogo", generateOrderVO.getBuyerLogo());
        params.put("consignee", generateOrderVO.getConsignee());
        params.put("shippingAddress", generateOrderVO.getShippingAddress());
        params.put("shippingPhone", generateOrderVO.getShippingPhone());
        params.put("tip", generateOrderVO.getTip());
        params.put("paymentType", generateOrderVO.getPaymentType());

        String orderList = JSON.toJSONString(generateOrderVO.getOrderVOList(), true);
        params.put("orderVOList", orderList);

        OkHttpUtil.post(generateOrderVO.getActivity(), URL.URL_GENERATE_ORDERS, "", params, generateOrderVO.getHandler(), MSG.MSG_GENERATE_ORDERS_SUCCESS, MSG.MSG_GENERATE_ORDERS_FIELD);

    }

    /**
     * 获取订单详情
     *
     * @param activity
     * @param orderNo  订单号
     * @param handler
     */
    public static void buyerOrderDetail(Activity activity, String orderNo, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("userId", UserXML.getUserId(activity));
        params.put("orderNo", orderNo);

        OkHttpUtil.post(activity, URL.URL_BUYER_ORDER_DETAIL, "", params, handler, MSG.MSG_BUYER_ORDER_DETAIL_SUCCESS, MSG.MSG_BUYER_ORDER_DETAIL_FIELD);
    }

    /**
     * 支付
     *
     * @param activity
     * @param paymentType 支付类型 0-线下支付，1-在线支付
     * @param orderNo     订单号
     * @param handler
     */
    public static void gotoThirdPayPage(Activity activity, String paymentType, String orderNo, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("paymentType", paymentType);
        params.put("orderNo", orderNo);

        OkHttpUtil.post(activity, URL.URL_GOTO_THIRD_PAY_PAGE, "", params, handler, MSG.MSG_GOTO_THIRD_PAY_PAGE_SUCCESS, MSG.MSG_GOTO_THIRD_PAY_PAGE_FIELD);

    }

    /**
     * 通过状态获取订单列表
     *
     * @param activity    -
     * @param queryStatus 10 - "待提货"-自提
     *                    11 - "待评价"-自提
     *                    12 - "已关闭"-自提
     *                    19 - "待付款"-自提
     *                    20 - "待发货"-邮寄
     *                    21 - "待收货"-邮寄
     *                    22 - "待评价"-邮寄
     *                    23 - "已关闭"-邮寄
     *                    29 - "待付款"-邮寄
     *                    39 - 全部代付款 不区分自提和邮寄
     * @param page        当前页
     * @param limit       每页限制数量
     * @param handler     -
     */
    public static void getBuyerOrderByStatus(Activity activity, String queryStatus, String page, String limit, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("buyerId", UserXML.getUserId(activity));
        params.put("queryStatus", queryStatus);
        params.put("page", page);
        params.put("limit", limit);
        params.put("shippingType", "2"); //      * @param shippingType 0-不需发货 1-自提 2-邮寄

        OkHttpUtil.post(activity, URL.URL_GET_BUYER_ORDER_BY_STATUS, "", params, handler, MSG.MSG_GET_BUYER_ORDER_BY_STATUS_SUCCESS, MSG.MSG_GET_BUYER_ORDER_BY_STATUS_FIELD);

    }

    /**
     * 获取所有状态订单列表
     *
     * @param activity
     * @param page
     * @param limit
     * @param handler
     */
    public static void getOrderListAll(Activity activity, String page, String limit, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("buyerId", UserXML.getUserId(activity));
        params.put("page", page);
        params.put("limit", limit);
        params.put("shippingType", "2"); //      * @param shippingType 0-不需发货 1-自提 2-邮寄

        OkHttpUtil.post(activity, URL.URL_GET_ORDER_LIST_AGSRV, "", params, handler, MSG.MSG_GET_BUYER_ORDER_BY_STATUS_SUCCESS, MSG.MSG_GET_BUYER_ORDER_BY_STATUS_FIELD);

    }


    /**
     * 取消订单
     *
     * @param activity
     * @param orderNo      订单编号
     * @param cancelReason 取消理由
     * @param handler
     */
    public static void cancelOrder(Activity activity, String orderNo, String cancelReason, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("userId", UserXML.getUserId(activity));
        params.put("orderNo", orderNo);
        params.put("cancelReason", cancelReason);

        OkHttpUtil.post(activity, URL.URL_CANCEL_ORDER_AGSRV, "", params, handler, MSG.MSG_CANCEL_ORDER_AGSRV_SUCCESS, MSG.MSG_CANCEL_ORDER_AGSRV_FIELD);

    }

    /**
     * 删除订单
     *
     * @param activity
     * @param orderNo  订单号
     * @param handler
     */
    public static void deleteOrder(Activity activity, String orderNo, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("userId", UserXML.getUserId(activity));
        params.put("orderNo", orderNo);

        OkHttpUtil.post(activity, URL.URL_DELETE_ORDER_AGSRV, "", params, handler, MSG.MSG_DELETE_ORDER_AGSRV_SUCCESS, MSG.MSG_DELETE_ORDER_AGSRV_FIELD);

    }

    /**
     * 确认收货订单
     *
     * @param activity
     * @param orderNo  订单号
     * @param handler
     */
    public static void affirmReceipt(Activity activity, String orderNo, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("userId", UserXML.getUserId(activity));
        params.put("orderNo", orderNo);

        OkHttpUtil.post(activity, URL.URL_AFFIRM_RECEIPT, "", params, handler, MSG.MSG_AFFIRM_RECEIPT_SUCCESS, MSG.MSG_AFFIRM_RECEIPT_FIELD);

    }

    /**
     * 查看物流
     *
     * @param activity
     * @param orderNo  订单号
     * @param handler
     */
    public static void getShippingInfo(Activity activity, String orderNo, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("orderNo", orderNo);

        OkHttpUtil.post(activity, URL.URL_GET_SHIPPING_INFO, "", params, handler, MSG.MSG_GET_SHIPPING_INFO_SUCCESS, MSG.MSG_GET_SHIPPING_INFO_FIELD);

    }

    /**
     * 评价订单接口
     *
     * @param activity
     * @param orderNo            订单号
     * @param accordStore        符合度评分
     * @param serviceScore       服务态度评分
     * @param isAnonymity        是否匿名 1 是 0否
     * @param evaluateProductStr 商品评价列表json
     * @param handler
     */
    public static void mobileEvaluateOrder(Activity activity, String orderNo, String accordStore, String serviceScore, String isAnonymity, String evaluateProductStr, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("orderNo", orderNo);
        params.put("accordStore", accordStore);
        params.put("serviceScore", serviceScore);
        params.put("isAnonymity", isAnonymity);
        params.put("evaluateProductStr", evaluateProductStr);
        params.put("useraccId", UserXML.getUserId(activity));

        OkHttpUtil.post(activity, URL.URL_MOBILE_EVALUATE_ORDER, "", params, handler, MSG.MSG_MOBILE_EVALUATE_ORDER_SUCCESS, MSG.MSG_MOBILE_EVALUATE_ORDER_FIELD);

    }

    /**
     * 物流信息查询
     *
     * @param activity
     * @param orderNo  订单号
     * @param handler
     */
    public static void getLogisticsInfo(Activity activity, String orderNo, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("orderNo", orderNo);

        OkHttpUtil.post(activity, URL.URL_GET_LOGISTICS_INFO, "", params, handler, MSG.MSG_GET_LOGISTICS_INFO_SUCCESS, MSG.MSG_GET_LOGISTICS_INFO_FIELD);

    }


    /**
     * 设置购物项的数量
     *
     * @param activity
     * @param shoppingCartItemId 商品购物项id
     * @param num                数量
     * @param handler
     */
    public static void setShoppingCartItemQuantities(Activity activity, String shoppingCartItemId, String num, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("shoppingCartItemId", shoppingCartItemId);
        params.put("num", num);

        OkHttpUtil.post(activity, URL.URL_SET_SHOPPING_CART_ITEM_QUANTITIES, "", params, handler, MSG.MSG_SET_SHOPPING_CART_ITEM_QUANTITIES_SUCCESS, MSG.MSG_SET_SHOPPING_CART_ITEM_QUANTITIES_FIELD);

    }

}
